//
//  CYAddPersonTVC.m
//  CreditAddressBook
//
//  Created by Lee on 15/9/22.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYAddPersonTVC.h"
#import "CYTool.h"
#import "CYAddressBookTool.h"
#import "RHPerson.h"
#import "CYContacts.h"
#import "CNMutableContact+Extension.h"

@interface CYAddPersonTVC ()

@end

@implementation CYAddPersonTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableView];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == CYIndexFirst) {//新增联系人
//        RHPerson *person = nil;
//        if (self.phone) {
//            person = [CYAddressBookTool newRHPersonWithName:nil phones:@[self.phone]];
//        }
//        [CYAddressBookTool ABNewPersonWithController:self person:person completion:^(RHPerson *person) {
//            CYAlertView(@"新增成功");
//        }];
        
//        CNMutableContact *contact = nil;
        CYContacts *contacts = [[CYContacts alloc] init];
        if (self.phone) {
#if IS_IOS9
            CNMutableContact *contact = [[CNMutableContact alloc] init];
            contact.phones = @[self.phone];
            contacts.contact = contact;
            contacts.person = [CYAddressBookTool newRHPersonWithName:nil phones:@[self.phone]];
#endif
        }
        [CYAddressBookTool newPersonWithController:self person:contacts completion:^(id obj) {
            //发出通知
            CYContacts *userInfo = [CYContacts contactsWithPerson:obj];
            [CYNotificationCenter postNotificationName:CYAddPersonNotification object:nil userInfo:@{@"userInfo": userInfo}];
        }];
//        [CYAddressBookTool CNNewPersonWithController:self person:contacts.contact completion:^(CNContact *contact) {
//            CYAlertView(@"新增成功");
//            [CYNotificationCenter postNotificationName:CYAddPersonNotification object:nil userInfo:<#(nullable NSDictionary *)#>];
//        }];
//        [CYAddressBookTool newPersonWithController:self person:contacts completion:^(id obj) {
//            CYAlertView(@"新增成功");
//        }];
        
    } else {//添加都现有联系人
//        [CYAddressBookTool ABAddressBookUIPickerWithController:self edit:NO completion:^(RHPerson *person) {
//            NSString *phone = self.phone;
//            if (phone) {
//                RHMutableMultiStringValue *value = person.phoneNumbers.mutableCopy;
//                [value addValue:phone withLabel:RHPersonPhoneIPhoneLabel];
//                person.phoneNumbers = value;
//                [CYAddressBookTool ABNewPersonWithController:self person:person completion:^(RHPerson *person) {
//                    //发出通知更新通话记录显示
//                    [CYNotificationCenter postNotificationName:CYCallRecordChangeNotification object:nil];
//                    CYAlertView(@"添加成功");
//                }];
//            }
//        }];
        
//        [CYAddressBookTool CNAddressBookUIPickerWithController:self edit:YES completion:^(CNContact *contact) {
//            if (self.phone) {
//                NSMutableArray *phones = [NSMutableArray arrayWithArray:contact.phoneNumbers];
//                CNLabeledValue *value = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberiPhone value:[CNPhoneNumber phoneNumberWithStringValue:self.phone]];
//                [phones addObject:value];
//                CNMutableContact *mutableContact = contact.mutableCopy;
//                mutableContact.phoneNumbers = phones;
//                
//                [CYAddressBookTool CNNewPersonWithController:self person:mutableContact completion:^(CNContact *contact) {
//                    CYAlertView(@"添加成功");
//                }];
//            }
//        }];
        
        [CYAddressBookTool addressBookUIPickerWithController:self edit:YES completion:^(id obj) {
            if (self.phone) {
                CYContacts *contacts = [[CYContacts alloc] init];
                if ([obj isKindOfClass:[RHPerson class]]) {
                    RHPerson *person = obj;
                    RHMutableMultiStringValue *value = person.phoneNumbers.mutableCopy;
                    [value addValue:self.phone withLabel:RHPersonPhoneIPhoneLabel];
                    person.phoneNumbers = value;
                    contacts.person = person;
                } else {
#if IS_IOS9
                    if ([obj isKindOfClass:[CNContact class]]) {
                        CNContact *contac = obj;
                        CNMutableContact *contact = contac.mutableCopy;
                        contact.phones = @[self.phone];
                        contacts.contact = contact;
                    }
#endif
                }
                [CYAddressBookTool newPersonWithController:self person:contacts completion:^(id obj) {
                    //发出通知
                    CYContacts *userInfo = [CYContacts contactsWithPerson:obj];
                    [CYNotificationCenter postNotificationName:CYAddPersonNotification object:nil userInfo:@{@"userInfo": userInfo}];
                }];
            }
        }];
    }
}

@end
